function GAME:TileOccupied (x,y)
	print (x,y)
	if (!x) or (!y) or (x<1) or (x>8) or (y<1) or (y>8) then return end
	return self.Board[x][y]
end

function GAME:GetPossibleMoves (x,y,side,typ)
	if (!x) or (!y) then return end
	if self.OnlyAllowedPiece then
		if (x != self.OnlyAllowedPiece[1]) or (y != self.OnlyAllowedPiece[2]) then return {} end --no moves for other pieces
	end
	local posTbl = {}
	if typ == 1 then
		dir = 1
		if side == 2 then dir = -1 end --what direction the piece can move in
		for ydir = -1, 1, 2 do
			if x+dir >= 1 and x+dir <= 8 and y+ydir >= 1 and y+ydir <= 8 then
				print ("fine:", x+dir, y+ydir)
				local piece = self:TileOccupied (x+dir, y+ydir)
				if not piece then
					table.insert (posTbl, {x+dir, y+ydir})
				elseif piece[1] != side then
					local piece2 = self:TileOccupied (x+dir*2, y+ydir*2)
					if not piece2 then
						table.insert (posTbl, {x+dir*2, y+ydir*2})
					end
				end
			else
				print ("failed:", x+dir, y+ydir)
			end
		end
	else
		for dir = -1, 1, 2 do
			for ydir = -1, 1, 2 do
				if x+dir >= 1 and x+dir <= 8 and y+dir >= 1 and y+dir <= 8 then
					local piece = self:TileOccupied (x+dir, y+ydir)
					if not piece then
						table.insert (posTbl, {x+dir, y+ydir})
					elseif piece[1] != side then
						local piece2 = self:TileOccupied (x+dir*2, y+ydir*2)
						if not piece2 then
							table.insert (posTbl, {x+dir*2, y+ydir*2})
						end
					end
				else
					print ("failed:", x+dir, y+dir)
				end
			end
		end
	end
	return posTbl
end

function GAME:DoMove (sx,sy,x,y,send)
	print ("doing move", sx, sy, x, y)
	PrintTable (self.Board)
	if not self.Board[sx][sy] then
		print ("no piece to move")
		return
	end
	local jumped = false
	local sSide = self.Board[sx][sy][1]
	if math.abs (x - sx) == 2 then --jumped
		vx = (x + sx) / 2
		vy = (y + sy) / 2
		if self.Board[vx][vy] then
			--remove victim
			self.Board[vx][vy] = false
			local piece = self.Board[sx][sy]
			if #self:GetPossibleMoves (x,y,piece[1],piece[2]) > 0 then
				--print ("There could be more jumps?")
				PrintTable (self:GetPossibleMoves (x,y,piece[1],piece[2]))
				for k,v in pairs (self:GetPossibleMoves (x,y,piece[1],piece[2])) do
					if math.abs (x - v[1]) == 2 then
						--print ("Available jump!")
						jumped = true
						self.OnlyAllowedPiece = {x,y}
					end
				end
			else
				--print ("No more jumps.")
				self.OnlyAllowedPiece = false
			end
		end
	else
		self.OnlyAllowedPiece = false
	end
	if ((sSide == 1) and (x == 8)) or ((sSide == 2) and (x == 1)) then
		self.Board[sx][sy][2] = 2
	end
	self.Board[x][y] = table.Copy (self.Board[sx][sy])
	self.Board[sx][sy] = false
	self.SelectedPiece = false
	self.PossibleTiles = false
	if send and self.Opponent then
		AttemptToSay ("[DRAUGHTS] ["..self.Opponent:UserID().."] [MakeMove:"..sx..":"..sy..":"..x..":"..y.."]")
	end
	return jumped
end
